﻿#include <iostream>
using namespace std;
//简单的链表
struct ListNode
{
    int num;
    string val;
    struct ListNode* next;
};
int main()
{
    ListNode* head = nullptr, *tail = nullptr, *temp = nullptr;
    //分配第一个节点
    temp = new ListNode({ 1,"val1",nullptr});
    head = tail = temp;

    temp = new ListNode({ 2,"val2",nullptr });
    tail->next = temp; //上一个指针的next指向新的节点
    tail = temp;            //移动尾指针，指向新节点

    temp = new ListNode({ 3,"val3",nullptr });
    tail->next = temp; //上一个指针的next指向新的节点
    tail = temp;            //移动尾指针，指向新节点

    //遍历链表
    temp = head;        //从头指针开始
    while (temp != nullptr)
    {
        cout << "num=" << temp->num << " val=" << temp->val << " next=" << temp->next << endl;
        temp = temp->next;
    }

    //删除链表(从头节点开始)
    while (head != nullptr)
    {
        temp = head;
        head = head->next;
        delete temp;
    }
}
